#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    init();
    connect(ui->treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(treeItemChanged(QTreeWidgetItem*, int)));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::init()
{
    ui->treeWidget->clear();
    // first group
    QTreeWidgetItem *group = new QTreeWidgetItem(ui->treeWidget);
    group->setText(0, "group");
    group->setFlags(Qt::ItemIsUserCheckable |Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    group->setCheckState(0, Qt::Unchecked);
    group->setExpanded(true);
    // subitem 1
    QTreeWidgetItem *subItem1 = new QTreeWidgetItem(group);
    subItem1->setFlags(Qt::ItemIsUserCheckable |Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    subItem1->setText(0, "subItem1");
    subItem1->setCheckState(0, Qt::Unchecked);
    // subitem 2
    QTreeWidgetItem *subItem2 = new QTreeWidgetItem(group);
    subItem2->setFlags(Qt::ItemIsUserCheckable |Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    subItem2->setText(0, "subItem2");
    subItem2->setCheckState(0, Qt::Unchecked);
    // subitem 3
    QTreeWidgetItem *subItem3 = new QTreeWidgetItem(group);
    subItem3->setFlags(Qt::ItemIsUserCheckable |Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    subItem3->setText(0, "subItem3");
    subItem3->setCheckState(0, Qt::Unchecked);

}
void Widget::updateParentItem(QTreeWidgetItem *item)
{
    QTreeWidgetItem *parent = item->parent();
    if(parent == nullptr)
    {
        return;
    }
    int selectedCount = 0;
    int childCount = parent->childCount();
    for(int i = 0; i < childCount; i++)
    {
        QTreeWidgetItem *childItem = parent->child(i);
        if(childItem->checkState(0) == Qt::Checked)
        {
            selectedCount++;
        }
    }
    if(selectedCount <= 0)
    {
        parent->setCheckState(0, Qt::Unchecked);
    }
    else if(selectedCount == childCount)
    {
        parent->setCheckState(0, Qt::Checked);
    }
    else if(selectedCount > 0 && selectedCount < childCount)
    {
        parent->setCheckState(0, Qt::PartiallyChecked);
    }
}

void Widget::treeItemChanged(QTreeWidgetItem *item, int column)
{
    QString itemText = item->text(0);
    // checked
    if (Qt::Checked == item->checkState(0)) // selected
    {
        int count = item->childCount();
        if(count > 0) // group
        {
            for(int i = 0; i < count; i++)
            {
                // select subitem
                item->child(i)->setCheckState(0, Qt::Checked);
            }
        }
        else // subitem
        {
            updateParentItem(item);
        }
    }
    else if(Qt::Unchecked == item->checkState(0))
    {
        int count = item->childCount();
        if(count > 0)
        {
            for(int i = 0; i < count; i++)
            {
                item->child(i)->setCheckState(0, Qt::Unchecked);
            }
        }
        else
        {
            updateParentItem(item);
        }
    }
}

